<template>
  <div class="app-container">
  <el-button type="primary" @click="linkToAdd">新增右侧菜单</el-button>
  <el-button type="danger" v-show="showDelBtn" @click="delMenus">删除</el-button>
  <el-table
      v-loading="listLoading"
      :data="menus"
      element-loading-text="Loading"
      highlight-current-row
      @selection-change="handleSelectionChange">
  >
      <el-table-column
        type="selection"
        width="55">
      </el-table-column>
      <el-table-column align="center" label="ID" width="95">
        <template slot-scope="scope">
          {{ scope.row.id }}
        </template>
      </el-table-column>
      <el-table-column label="图片" width="300" align="center">
        <template slot-scope="scope">
          <el-image
            style="width: 100%;height:100px"
            :src="imgPath+scope.row.img"
            fit="fit"
          />
        </template>
      </el-table-column>
      <el-table-column label="显示名" width="300" align="center">
        <template slot-scope="scope">
          {{ scope.row.nam }}
        </template>
      </el-table-column>
      <el-table-column label="链接地址" width="300" align="center">
        <template slot-scope="scope">
          {{ scope.row.url }}
        </template>
      </el-table-column>
      <el-table-column label="排序号" width="95" align="center">
        <template slot-scope="scope">
          {{ scope.row.orderNum }}
        </template>
      </el-table-column>
      <el-table-column align="center" prop="created_at" label="创建时间" width="200">
        <template slot-scope="scope">
          <i class="el-icon-time" />
          <span>
            {{ scope.row.createTime }}
          </span>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="created_at" label="操作" width="200">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" circle @click="linkToEdit(scope.row.id)" />
          <el-button type="danger" icon="el-icon-delete" circle @click="delMenu(scope.row.id)" />
        </template>
      </el-table-column>
    </el-table>
    <nav style="text-align: center">
      <el-pagination
      background
      @current-change="handlePageChange"
      :current-page="pageNo"
      :page-size="pageSize"
      layout="prev, pager, next"
      :total="total">
    </el-pagination>
    </nav>
    
  </div>
  
</template>

<script>
import { getMenusApi, delMenuApi, delMenusApi } from '@/api/menu'
export default {
  data() {
    return {
      imgPath:process.env.VUE_APP_BASE_API,
      menu:{
          type: 2
      },
      pageNo: 1,
      pageSize: 10,
      menus: [],
      listLoading: true,
      delMenuArray:[],
      showDelBtn:false,
      total: 0
    }
  },
  created() {
    this.getMenus(this.pageNo, this.pageSize, this.menu)
  },
  methods: {
    handlePageChange(currentPage) {
      this.pageNo = currentPage;
      this.getMenus(this.pageNo, this.pageSize, this.menu)
    },
    handleSelectionChange(val) {
      if(val.length>0){
        this.showDelBtn=true
      }else{
        this.showDelBtn=false
      }
      this.delMenuArray = val
    },
    delMenus(){
      this.listLoading = true
      delMenusApi(this.delMenuArray)
        .then(response=>{
          this.getMenus(this.pageNo, this.pageSize)
          this.listLoading = false
        })
    },
    linkToAdd() {
      this.$router.push('/content/rightmenu/add')
    },
    onSearch(){
      this.pageNo = 1
      this.getMenus(this.pageNo, this.pageSize, this.menu)
    },
    onClear() {
      this.menu={}
      this.getMenus(this.pageNo, this.pageSize, this.menu)
    },
    getMenus(pageNo, pageSize, menu) {
      this.listLoading = true
      getMenusApi(pageNo, pageSize, this.menu)
        .then(response => {
          this.menus = response.data.list
          this.pageNo = response.data.pageNum
          this.total = response.data.total
          this.listLoading = false
        })
    },
    linkToEdit(id) {
      this.$router.push({name:'editrightmenu', params:{id:id}})
    },
    delMenu(id) {
      this.listLoading = true
      delMenuApi(id).then(response => {
        this.getMenus(this.pageNo, this.pageSize)
        this.listLoading = false
      })
    }
  }
}
</script>
